package qq.android.wan.qq.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import qq.android.wan.qq.R;
import qq.android.wan.qq.entity.FriendBean;

/**
 * Created by wxx on 2016/4/7.
 */
public class SearchAdapter extends BaseAdapter {
    private Context context;
    private List<FriendBean> datas;
    private String str_match;//要匹配的字符串
    private LayoutInflater layoutInflater;

    public SearchAdapter(Context context, List<FriendBean> datas, String str_match) {
        this.context = context;
        this.datas = datas;
        this.str_match = str_match;
        this.layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.adapter_search_contact, null);
            holder.tv_qqId = (TextView) convertView.findViewById(R.id.tv_qqId);
            holder.tv_text_contact = (TextView) convertView.findViewById(R.id.tv_text_contact);
            holder.view_line = convertView.findViewById(R.id.view_line);
            holder.tv_nick=(TextView)convertView.findViewById(R.id.tv_nick);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(position==0){
            holder.view_line.setVisibility(View.VISIBLE);
            holder.tv_text_contact.setVisibility(View.VISIBLE);
        }else {
            holder.view_line.setVisibility(View.GONE);
            holder.tv_text_contact.setVisibility(View.GONE);
        }
        FriendBean friendBean=datas.get(position);
        String userId=friendBean.getUserId()+"";
        String nick=friendBean.getNick();
        if(userId.contains(str_match)){
            int index = userId.indexOf(str_match);
            SpannableStringBuilder builder_qqId = new SpannableStringBuilder(userId);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
            builder_qqId.setSpan(colorSpan, index, index + str_match.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tv_qqId.setText(builder_qqId);
        }
        if(nick.contains(str_match)){
            int index = nick.indexOf(str_match);
            SpannableStringBuilder builder_qqId = new SpannableStringBuilder(nick);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
            builder_qqId.setSpan(colorSpan, index, index + str_match.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tv_nick.setText(builder_qqId);
        }


        return convertView;
    }

    protected class ViewHolder {
        private TextView tv_text_contact, tv_qqId,tv_nick;
        private View view_line;
    }
}
